<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!--返回错误信息-->
<?php echo validation_errors(); ?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php
    $title = [
        'title' => '换货单 - 编辑换货单',
    ];
    $this->load->view('/admin/common/header', $title);
    ?>
</head>

<body data-type="widgets">
<script src="/assets/js/theme.js"></script>
<div class="am-g tpl-g">

    <!--    头部   侧边菜单-->
    <?php $this->load->view('/admin/common/bodyLeft'); ?>
    <script type="text/javascript">
    	$(".js-sidebar-nav>li:nth-child(7)>ul").show(80);
    	$(".js-sidebar-nav>li:nth-child(7)>a>span").addClass("sidebar-nav-sub-ico-rotate");
    	$(".js-sidebar-nav>li:nth-child(7)>ul>li:nth-child(5)>a").addClass("active");
    </script>
    <!-- 内容区域 -->
    <div class="tpl-content-wrapper">
        <div class="row-content am-cf">
            <div class="row">
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                    <div class="widget am-cf">
                        <div class="widget-head am-cf">
                            <div class="widget-title  am-cf">
                                <ol class="am-breadcrumb am-breadcrumb-slash">
                                    <li><a href="/admin/procurement/index?page=1">采购管理</a></li>
                                    <li><a href="/admin/procurement/barterOrder?page=1">采购换货单</a></li>
                                    <li class="am-active">编辑换货单</li>
                                </ol>
                            </div>
                        </div>
                        <form method="post" enctype="multipart/form-data" class="am-form am-text-sm" id="submitForm">
                            <input type="hidden" name="id" value="<?php echo $list['id'] ?>">
                            <div class="widget-body  am-fr">
                                <div class="tpl-table-black-operation">
                                    <a href="javascript:void(0);" class="tpl-table-black-operation-del">
                                        基本信息
                                    </a>
                                </div>

                                <div style="margin-top: 20px">
                                    <div class="am-g">
                                        <!-- Row start -->
                                        <div class="am-u-sm-12">
                                                <div class="am-u-md-6">
                                                    <!--                                                    <form class="am-form am-text-sm">-->
                                                    <div class="am-form-group am-u-sm-12">
                                                            <label class="am-u-md-2 am-md-text-right am-padding-left-0 must"
                                                                   for="code">换货单编号</label>
                                                            <div class="am-u-md-10 ">
                                                                <input type="text" name="code" value="<?php echo $list['code'] ?>"
                                                                   id="code" readonly>
                                                            </div>
                                                            
                                                    </div>

                                                    <div class="am-form-group am-u-sm-12">
                                                            <label class="am-u-md-2 am-md-text-right am-padding-left-0 must"
                                                                   for="source">所属项目</label>
                                                            <div class="am-u-md-10 ">
                                                                <?php
                                                                if($list['status'] !=0 ){
                                                                    echo '<input  type="text" name="source" value='.$list['source'].' readonly>';
                                                                }else{
                                                                    echo '<select class="form-control" name="source" id="source" data-am-selected="{btnWidth: \'100%\',maxHeight: 200}">';
                                                                    foreach ($project as $v){
                                                                        if($v['name'] == $list['source']) {
                                                                            echo '<option value="'.$v['name'].'" selected>'.$v['name'].'</option>';
                                                                        }else{
                                                                            echo '<option value="'.$v['name'].'" >'.$v['name'].'</option>';
                                                                        }
                                                                    }
                                                                    echo '</select>';
                                                                }
                                                                ?>
                                                            </div>
                                                            
                                                    </div>

                                                    <div class="am-form-group am-u-sm-12">
                                                            <label class="am-u-md-2 am-md-text-right am-padding-left-0 must"
                                                                   for="createTime">单据时间</label>
                                                            <!-- <input class="am-u-md-10 form-control" name="createTime" value="<?php echo date('Y-m-d',$list['createTime']) ?>"
                                                                   id="createTime"> -->
                                                            <div class="am-u-md-10">
                                                                <input type="text" class=" am-form-field"  data-am-datepicker readonly  name="createTime" id="createTime" value="<?php echo date('Y-m-d',$list['createTime']) ?>" />
                                                            </div>
                                                    </div>

                                                    <div class="am-form-group am-u-sm-12">
                                                            <label class="am-u-md-2 am-md-text-right am-padding-left-0 must"
                                                                   for="content">申请原因</label>
                                                            <div class="am-u-md-10 ">
                                                                 <textarea rows="5" name="content"
                                                                      id="content"><?php echo $list['content']?></textarea>
                                                            </div>
                                                           
                                                    </div>
                                                    <!--                                                    </form>-->
                                                </div>

                                                <div class="am-u-md-6">
                                                    <!--                                                    <form class="am-form am-text-sm">-->
                                                    <div class="am-form-group am-u-sm-12">
                                                            <label class="am-u-md-2 am-md-text-right am-padding-left-0 must"
                                                                   for="name">换货单名称</label>
                                                            <div class="am-u-md-10 ">
                                                                <input type="text" name="name" value="<?php echo $list['name']?>"
                                                                   id="name">
                                                            </div>
                                                            
                                                    </div>

                                                    <div class="am-form-group am-u-sm-12">
                                                            <label class="am-u-md-2 am-md-text-right am-padding-left-0 must"
                                                                   for="supplier">供应商</label>
                                                            <div class="am-u-md-10 ">
                                                                <?php
                                                                if($list['status'] !=0 ){
                                                                    echo '<input type="text" name="supplier" value="'.$list['supplier'].'">';
                                                                }else{
                                                                    echo '<select class="form-control" name="supplier" id="supplier" data-am-selected="{btnWidth: \'100%\',maxHeight: 200}">';
                                                                    foreach ($supplier as $v2){
                                                                        if($v2['name'] == $list['supplier']){
                                                                            echo '<option value="'.$v2['name'].'" selected>'.$v2['name'].'</option>';

                                                                        }else{
                                                                            echo '<option value="'.$v2['name'].'">'.$v2['name'].'</option>';
                                                                        }
                                                                    }
                                                                    echo '</select>';
                                                                }
                                                                ?>
                                                                
                                                            </div>
                                                            
                                                    </div>

                                                    <div class="am-form-group am-u-sm-12">
                                                            <label class="am-u-md-2 am-md-text-right am-padding-left-0"
                                                                   for="endTime">期望到货时间</label>
                                                            <div class="am-u-md-10">
                                                                <input type="text" class=" am-form-field" placeholder="选择期望到货时间" data-am-datepicker  name="endTime" id="endTime" readonly value="<?php echo date('Y-m-d',$list['endTime'])?>" />
                                                            </div>
                                                    </div>


                                                    <input type="hidden" name="type" value="采购换货单">

                                                    <!--                                                    </form>-->
                                                </div>
                                        </div>
                                        <!-- Row end -->
                                    </div>
                                    <hr>
                                </div>

                                <div class="tpl-table-black-operation">
                                    <a href="javascript:void(0);" class="tpl-table-black-operation-del">
                                        详细信息
                                    </a>
                                </div>

                                <!--                                 更换为goodsMessage 数据-->
                                <div style="margin-top: 20px">
                                    <div class="am-u-sm-12">
                                        <table width="100%"
                                               class="am-table am-table-compact am-table-striped tpl-table-black "
                                               id="example-r">
                                            <thead>
                                            <tr>
                                                <th style="width: 5%">序号</th>
                                                <th style="width: 15%">商品编码</th>
                                                <th style="width: 10%">商品名称</th>
                                                <th style="width: 13%">商品规格</th>
                                                <th style="width: 5%">单位</th>
                                                <th style="width: 8%">退货数量</th>
                                                <th style="width: 8%">退货单价(元)</th>
                                                <th style="width: 10%">退货金额(元)</th>
                                                <th style="width: 18%">备注</th>
                                                <th style="width: 8%">操作</th>
                                            </tr>
                                            </thead>
                                            <tbody id="goodsMessage">
                                            <?php

                                              	foreach ($list['goodsMessage'] as $k=>$v){
                                              		$v['ckId'] = isset($v['ckId']) ? ($v['ckId']) : '';	
                                                        echo '<tr class="line">
                                              
                                                   <td class="order">'.$v['order'].'</td>
                                                <td data-id="'.$v['dataId'].'" data-ckid="'.$v['ckId'].'" class="product_no">'.$v['product_no'].'</td>
                                                <td class="name">'.$v['name'].'</td>
                                                <td class="specification"   >'.html_entity_decode($v['specification']).'</td>
                                                <td class="goodsUnit">'.$v['goodsUnit'].'</td>';

                                                  if($list['status'] != 0 || strstr($v['specification'], '商品规格+')){
                                                            echo '<td>
                                                    <div class="am-form">
                                                        <input type="text" name="" class="am-u-md-12 form-control " value="'.$v['quantity'].'" readonly>
                                                    </div>
                                                </td>
                                                <td >
                                                    <div class="am-form">
                                                        <input type="text" name="" class="am-u-md-12 form-control " value="'.$v['price'].'" readonly>
                                                    </div>
                                                </td>
                                                <td class="price-all">'.$v['priceAll'].'</td>
                                                <td>
                                                    <div class="am-form">
                                                        <input type="text" name="" class="am-u-md-12 form-control " value="'.$v['remarks'].'" readonly>
                                                    </div>
                                                </td>
                                                <td>
                                                    <button type="button" class="am-btn am-btn-danger am-btn-xs delect-btn" disabled="true">删除</button>
                                                </td>
                                            </tr>';
                                                        }else{
                                                            echo '          <td>
                                                    <div class="am-form">
                                                        <input type="text" name="" class="am-u-md-12 form-control quantity" value="'.$v['quantity'].'" readonly>
                                                    </div>
                                                </td>
                                                <td>
                                                    <div class="am-form">
                                                        <input type="text" name="" class="am-u-md-12 form-control price" value="'.$v['price'].'" readonly>
                                                    </div>
                                                </td>
                                                <td class="price-all">'.$v['priceAll'].'</td>

                                                <td>
                                                    <div class="am-form">
                                                        <input type="text" name="" class="am-u-md-12 form-control remarks" value="'.$v['remarks'].'" readonly>
                                                    </div>
                                                </td>
                                                <td>
                                                    <button type="button" class="am-btn am-btn-danger am-btn-xs delect-btn" disabled="true">删除</button>
                                                </td>
                                            </tr>';
                                                      }

                                                    }


                                                ?>
                                            </tbody>
                                        </table>
                                        <button type="button" class="am-btn am-btn-secondary"  data-am-modal="{target: '#changeCommodity', closeViaDimmer: 0, width: 900, height: 500}" id="chooseCommodity" disabled="true">选择商品</button>
                                        <div class="am-fr" style="font-size: 16px;line-height: 37px">
                                            共<span id="class"><?php $attribute = explode('/',$list['attribute']);echo $attribute[0].'</span>类,'.'<span id="quantity-all">'.$attribute[1].'</span>件商品';?>


                                        </div>
                                        <hr>
                                    </div>
                                </div>

                                <div class="tpl-table-black-operation">
                                    <a href="javascript:void(0);" class="tpl-table-black-operation-del">
                                        选择审批人
                                    </a>
                                </div>
                                <div class="am-u-sm-12">
                                    <button id="sure" type="button" style="margin: 18px 0" class="am-btn am-btn-secondary">重选商品</button>
                                </div>
                                <div style="margin-top: 20px">
                                    <div class="am-g">
                                        <div class="am-u-sm-4">
                                            <div class="am-form-group am-u-sm-12 approver">
                                                <label class="must am-u-md-4" >选择审批人：</label>
                                                <label class="am-u-md-4 am-md-text-right am-padding-left-0" for="manager">
                                                    一级审批人
                                                </label>
                                                <div class="am-u-md-4">
                                                    <?php
                                                                if($list['status']){
                                                                    echo '<select data-am-selected="{btnSize: \'sm\'}" name="manager" id="manager" disabled>
                                                        <option value="">请选择一级审批人</option>
                                                    </select>';
                                                                }else{
                                                                    echo '<select data-am-selected="{btnSize: \'sm\'}" name="manager" id="manager">
                                                        <option value="">请选择一级审批人</option>
                                                    </select>';
                                                                } 
                                                           ?>
                                                </div>
                                                    
                                            </div>
                                            <div class="am-form-group am-u-sm-12 approver">
                                                <label class=" am-u-md-4" ></label>

                                                <label class="am-u-md-4 am-md-text-right am-padding-left-0"
                                                       for="safeguard">二级审批人</label>
                                                <div class="am-u-md-4">
                                                    <?php
                                                                if($list['status']){
                                                                    echo '<select data-am-selected="{btnSize: \'sm\'}" name="safeguard" id="safeguard" disabled>
                                                        <option value="">请选择二级审批人</option>
                                                    </select>';
                                                                }else{
                                                                    echo '<select data-am-selected="{btnSize: \'sm\'}" name="safeguard" id="safeguard">
                                                        <option value="">请选择二级审批人</option>
                                                    </select>';
                                                                } 
                                                           ?>
                                                </div>
                                            </div>
                                            <div class="am-form-group am-u-sm-12 approver">
                                                <label class="am-u-sm-4"></label>
 
                                                <label class="am-u-md-4 am-md-text-right am-padding-left-0"
                                                       for="deputy">三级审批人</label>
                                                <div class="am-u-sm-4">
                                                   <?php
                                                                if($list['status']){
                                                                    echo '<select data-am-selected="{btnSize: \'sm\'}" name="deputy" id="deputy" disabled>
                                                        <option value="">请选择三级审批人</option>
                                                    </select>';
                                                                }else{
                                                                    echo '<select data-am-selected="{btnSize: \'sm\'}" name="deputy" id="deputy">
                                                        <option value="">请选择三级审批人</option>
                                                    </select>';
                                                                } 
                                                           ?>
                                                </div> 
                                            </div>
                                            <div class="am-form-group am-u-sm-12 approver">
                                                <label class="am-u-sm-4"></label>
 
                                                <label class="am-u-md-4 am-md-text-right am-padding-left-0"
                                                       for="fdeputy">四级审批人</label>
                                                <div class="am-u-sm-4">
                                                   <?php
                                                                if($list['status']){
                                                                    echo '<select data-am-selected="{btnSize: \'sm\'}" name="fdeputy" id="fdeputy" disabled>
                                                        <option value="">请选择四级审批人</option>
                                                    </select>';
                                                                }else{
                                                                    echo '<select data-am-selected="{btnSize: \'sm\'}" name="fdeputy" id="fdeputy">
                                                        <option value="">请选择四级审批人</option>
                                                    </select>';
                                                                } 
                                                           ?>
                                                </div> 
                                            </div>
                                            <div class="am-form-group am-u-sm-12 approver">
                                                <label class="am-u-sm-4"></label>
 
                                                <label class="am-u-md-4 am-md-text-right am-padding-left-0"
                                                       for="chairman">五级审批人</label>
                                                <div class="am-u-sm-4">
                                                    <?php
                                                                if($list['status']){
                                                                    echo '<select data-am-selected="{btnSize: \'sm\'}" name="chairman" id="chairman" disabled>
                                                        <option value="">请选择五级审批人</option>
                                                    </select>';
                                                                }else{
                                                                    echo '<select data-am-selected="{btnSize: \'sm\'}" name="chairman" id="chairman">
                                                        <option value="">请选择五级审批人</option>
                                                    </select>';
                                                                } 
                                                           ?>
                                                </div> 
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                
                            </div>
                            <div class="btn-box">
                                    <?php
                                    if($list['status']){
                                        echo '<button onclick="back()" type="button" class="am-btn am-btn-primary tpl-btn-bg-color-success" style="width:220px;margin: 20px;">取消</button>';
                                    }else{
                                        echo '
                                <input type="submit" value="提交" class="am-btn am-btn-primary tpl-btn-bg-color-success commit" style="width:220px;margin: 20px;">
                                <button onclick="back()" type="button" class="am-btn am-btn-primary tpl-btn-bg-color-success" style="width:220px">取消</button>
                                ';
                                    }
                                    ?>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- 模态框 -->
<div class="am-modal am-modal-no-btn" tabindex="-1" id="addSpecification">
    <div class="am-modal-dialog">
        <div class="am-modal-hd">选择规格
            <a href="javascript: void(0)" class="am-close am-close-spin" data-am-modal-close>&times;</a>
        </div>
        <div class="am-modal-bd">
            <table class="am-table am-table-bordered am-table-radius am-table-striped am-table-hover">
                <thead>
                    <tr>
                        <th style="width: 60%">规格</th>
                        <th style="width: 40%">换货数量</th>
                    </tr>
                </thead>
                <tbody class="specification-box">
                </tbody>
            </table>
        </div>
        <div class="btn-box">
            <button type="button" class="am-btn am-btn-primary" id="confirmSpecification" data-am-modal-close>确认</button>
        </div>
    </div>
</div>
<!-- 模态框 -->
<div class="am-modal am-modal-no-btn" tabindex="-1" id="changeCommodity">
  <div class="am-modal-dialog">
    <div class="am-modal-hd">选择商品
      <a href="javascript: void(0)" class="am-close am-close-spin" data-am-modal-close>&times;</a>
    </div>
    <div class="am-modal-bd">
        <div class="fl">
            <div class="box">
                <div class="list-title">
                    所有分类
                </div>
                <div class="left-list">

                </div>
            </div>
        </div>
        <div class="fr goods-details">
            <form class="am-form am-form-horizontal">
                <div class="am-form-group search-box">
                    <div class="am-u-sm-8 search-input">
                        <input type="text" id="searchValue" placeholder="请输入商品名称查询。">
                    </div>
                    <div class="am-u-sm-2" >
                        <button type="button" class="am-btn am-btn-success" id="searchCommodity">查询</button>
                    </div>
                    <div class="am-u-sm-2">
                        <button type="button" class="am-btn am-btn-primary" id="addCommodity" data-am-modal-close>确认</button>
                    </div>
                </div>
            </form>
            <div class="am-u-sm-12 table-box">
                <table class="am-table am-table-bordered am-table-radius am-table-striped am-table-hover">
                    <thead>
                        <tr>
                            <th style="width: 5%;"><input type="checkbox" class="check-all" name=""></th>
                            <th style="width: 10%;">序号</th>
                            <th style="width: 25%;">商品编号</th>
                            <th style="width: 25%;">商品名称</th>
                            <th style="width: 10%;">单位</th>
                            <th style="width: 15%;">缺货数量</th>
                        </tr>
                    </thead>
                    <tbody class="tr-box">

                    </tbody>
                </table>
            </div>

            <div class="pagination-box">
                <ul class="am-pagination" id="pagination">
                </ul>
            </div>
        </div>
    </div>
</div>


<script src="/assets/js/amazeui.min.js"></script>
<script src="/assets/js/amazeui.datatables.min.js"></script>
<script src="/assets/js/dataTables.responsive.min.js"></script>
<script src="/assets/js/app.js"></script>
<script src="/assets/js/zdy_form.js"></script>
<script src="/assets/js/page.js"></script>
<script src="/assets/js/procurement.js"></script>

<script>
    $(function(){
        var amount = '<?php echo $list['amount'] ?>',
            manager = '<?php echo $list['manager'] ?>',
            safeguard = '<?php echo $list['safeguard'] ?>',
            deputy = '<?php echo $list['deputy'] ?>',
            fdeputy = '<?php echo $list['fdeputy'] ?>',
            chairman = '<?php echo $list['chairman'] ?>';
        selectApprover = true;
        //添加审批抄送并默认选中
        addAudit("/admin/user/oneLevel","#manager",manager);
        addAudit("/admin/user/twoLevel","#safeguard",safeguard);
        addAudit("/admin/user/threeLevel", "#deputy",deputy);
        addAudit("/admin/user/fourLevel", "#fdeputy",fdeputy);
        addAudit("/admin/user/fiveLevel", "#chairman",chairman);
        //显示几个审批人
        editShowAudit(amount);
        calcPriceAndNum();
        requestSpecification();

        //点击选择商品
        $("#chooseCommodity").click(function(){
            initCategory();
            levelClick(0,0,1);
        });

        //点击查询 商品按钮
        $("#searchCommodity").on("click",function(){
            var search = $("#searchValue").val();
            if(!search){
                alert("请输入商品名称查询。");
                return false;
            }
            levelClick(0,0,1,search);
        });

        //点击确认
        var max = 0;
        var lineorder = $("#goodsMessage").find(".order");
        var maxorder = 0;
        for(var i = 0;i < lineorder.length; i++){
            maxorder = lineorder.eq(i).html() == "" ? 0 : parseInt(lineorder.eq(i).html());
            max = Math.max(max,maxorder);
        }

        $("#addCommodity").click(function(){
            var whichCheck = $(".check-one:checked");
            var tr='';
            for(var i = 0;i < whichCheck.length; i++){
                var priceAll = '';
                var product_no = whichCheck.eq(i).parent().siblings(".product_no").text();
                var name = whichCheck.eq(i).parent().siblings(".name").text();
                var goodsUnit = whichCheck.eq(i).parent().siblings(".goodsUnit").text();
                var dataId = whichCheck.eq(i).data("id");
                var ckId = whichCheck.eq(i).data("ckid");
                var stockpriceid = whichCheck.eq(i).data("stockpriceid");
                var classNum;    //类数量
                var hasSpecification = whichCheck.eq(i).data("has");  //是否有规格  1：有  0：无
                max++;
                classNum = max;
                if(max < 10){
                    max = "0" + max;
                }
                tr += '<tr class="line"><td class="order">'+ max +'</td>'
                +'<td data-id="'+ dataId +'" data-ckid="'+ ckId +'" class="product_no">'+ product_no +'</td>'
                +'<td class="name">'+ name +'</td>';
                if(hasSpecification == 1){
                    tr += '<td class="specification"><button type="button" class="am-btn am-btn-default am-radius specification-btn" data-am-modal="{target: \'#addSpecification\', closeViaDimmer: 0, width: 500, height: 400}">商品规格+</button></td>'
                        + '<td class="goodsUnit">'+ goodsUnit +'</td>'
                        +'<td><div class="am-form"><input type="text" name="" class="am-u-md-12 form-control " readonly></div></td>'
                        +'<td><div class="am-form"><input type="text" name="" class="am-u-md-12 form-control " readonly></div></td>'
                        +'<td class="price-all">'+ priceAll +'</td>'
                        +'<td><div class="am-form"><input type="text" name="" class="am-u-md-12 form-control "></div></td>'
                        + '<td><button type="button" class="am-btn am-btn-danger am-btn-xs delect-btn">删除</button></td>'
                        +'</tr>';
                }else{
                    tr += '<td class="specification" data-id="'+ stockpriceid +'"></td>'
                        + '<td class="goodsUnit">'+ goodsUnit +'</td>'
                        +'<td><div class="am-form"><input type="text" name="" class="am-u-md-12 form-control quantity"></div></td>'
                        +'<td><div class="am-form"><input type="text" name="" class="am-u-md-12 form-control price"></div></td>'
                        +'<td class="price-all">'+ priceAll +'</td>'
                        +'<td><div class="am-form"><input type="text" name="" class="am-u-md-12 form-control remarks"></div></td>'
                        + '<td><button type="button" class="am-btn am-btn-danger am-btn-xs delect-btn">删除</button></td>'
                        +'</tr>';
                }
                
            }
            $("#goodsMessage").append(tr);
            $("#class").text(classNum);  //多少类商品


            //请求规格
            requestSpecification();
            
            calcPriceAndNum();
        });

        if(<?php echo $list['status'] ?> != 0){

            $(".specification-btn").attr("disabled","disabled");
            $("#chooseCommodity").attr("disabled","disabled");
            $("input").attr("readonly","readonly");
            $("textarea").attr("readonly","readonly");
            $("#endTime").attr("disabled","disabled");
            $("#createTime").attr("disabled","disabled");

        }


        var lockSubmit = false;
        $(".commit").bind("click",function(e){
            e.preventDefault();

            if(lockSubmit){
                return false;
            }
            lockSubmit =true;

            if(!selectApprover){
                $("#pop-up-content").text("请选择审批人");
                $("#my-alert").modal();
                return false;
            };

            // 表单验证
            var ruleObj = {
                rules:{
                    code:{required:true},
                    name:{required:true},
                    source:{required:true},
                    supplier:{required:true},
                    createTime:{required:true},
                    content:{required:true},
                    manager:{required:true},
                    safeguard:{required:true},
                    deputy:{required:true}
                }, message:{
                    code:{required:"请填写退货单编号"},
                    name:{required:"请填写退货单名称"},
                    source:{required:"请填写所属项目"},
                    supplier:{required:"请填写供应商"},
                    createTime:{required:"请填写单据时间"},
                    content:{required:"请填写退货原因"},
                    manager:{required:"请填写一级审批人"},
                    safeguard:{required:"请填写二级审批人"},
                    deputy:{required:"请填写三级审批人"}
                }   
            };

            if (piseCase == 1) {
                var valid = zdy.validate.valid(ruleObj);
            } else if (piseCase == 2) {
                ruleObj = $.extend( true, ruleObj, {
                    rules:{
                        fdeputy:{required:true}
                    }, message:{
                        fdeputy:{required:"请填写四级审批人"}
                    } 
                } );
                var valid = zdy.validate.valid(ruleObj);
            } else if (piseCase == 3) {
                ruleObj = $.extend( true, ruleObj, {
                    rules:{
                        fdeputy: {required:true},
                        chairman: {required:true}
                    }, message:{
                        fdeputy:{required:"请填写四级审批人"},
                        chairman:{required:"请填写五级审批人"}
                    } 
                } );
                var valid = zdy.validate.valid(ruleObj);
            }
            if (!valid) {
                lockSubmit = false;
                return;
            }


            var formData = new FormData($("#submitForm")[0]);
            var action = $(this).val();
            var attribute = $("#class").text() + "/" + $("#quantity-all").text();
            var goodsMessage = getGoodsMessage();
            var endTime  = $("#endTime").val();

            if (!endTime) {
                formData.delete("endTime");
                formData.append("endTime","未填写期望到货时间");
            }
            formData.append("attribute",attribute);
            formData.append("action",action);
            formData.append("amount",getAllPrice());
            formData.append("goodsMessage",goodsMessage);
            $.ajax({
                type: "POST",
                url : '/admin/procurement/returnOrderEdit',
                data: formData,
                cache: false,
                processData: false,
                contentType: false,
                success: function(data){
                    $("#pop-up-content").text(data);
                    $("#my-alert").modal();
                    window.location.href = '/admin/procurement/barterOrder?page=1';

                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.readyState);
                    alert(textStatus);
                }
            });
            return false;
        });

        //全选反选
        $("#changeCommodity .check-all").click(function(){
                $("#changeCommodity .check-one").prop("checked",this.checked);
        });
        
    });

    function requestSpecification() {
        $(".specification-btn").unbind("click").click(function(){
            var thisTr = $(this).parents(".line");                   //点击的本行tr
            var productId = thisTr.find(".product_no").data("id");   //传给后台的商品ID
            var ckId = thisTr.find(".product_no").data("ckid");   //传给后台的商品ID
            var product_no = thisTr.find(".product_no").text();   //商品编码
            var name = thisTr.find(".name").text();               //名字
            var goodsUnit = thisTr.find(".goodsUnit").text();     //单位
            var specificationData;                                  //请求规格传给后台的ID
            specificationData = ckId ? ckId : productId;
            $.ajax({    
                url:"/api/goods/getGoodsByStock",
                data:{
                    productId:specificationData
                },
                dataType:"json",
                type:"post",
                async:false,
                success:function(data){
                    var specificationBox = $(".specification-box");
                    var tr='';
                    for(var i = 0; i < data.length; i++){
                        var specificationId = data[i].id;
                        var specificationArray = data[i].stock.join();
                        tr += '<tr data-id="'+ specificationId +'">'
                              + '<td class="specification-value">'+ specificationArray +'</td>'
                              + '<td>'
                                + '<div class="am-form">'
                                    + '<input type="text" name="" class="">'
                                + '</div>'
                              + '</td>'
                            + '</tr>';
                    }
                    specificationBox.html(tr);
                },
                error:function(data){
                    console.log(data);
                }
            });

            // 点击确定添加新规格商品
            $("#confirmSpecification").unbind("click").click(function(){
                var stockInput = $(".specification-box").find("input");
                var specificationTr = '';
                var oneGoodsNum = 0;

                for(var i = 0;i < stockInput.length; i++){
                    if(stockInput.eq(i).val() == 0 || stockInput.eq(i).val().replace(/(^\s*)|(\s*$)/g, "")==""){
                        continue;
                    }
                    var stockInputVal = stockInput.eq(i).val();  //进货数量
                    var specificationVal = stockInput.eq(i).parents("tr").find(".specification-value").text(); //规格
                    var stockPriceId = stockInput.eq(i).parents("tr").data("id");
                    specificationTr += '<tr class="line">'
                                        + '<td class="orde  r"></td><td class="product_no" data-id="'+ productId +'">'+ product_no +'</td>'
                                        + '<td class="name">'+ name +'</td>'
                                        + '<td class="specification" data-id="'+ stockPriceId +'">'+ specificationVal +'</td>'
                                        + '<td class="goodsUnit">'+ goodsUnit +'</td>'
                                        + '<td><div class="am-form"><input type="text" name="" value="'+ stockInputVal +'" class="am-u-md-12 form-control quantity"></div></td>'
                                        + '<td><div class="am-form"><input type="text" name="" class="am-u-md-12 form-control price"></div></td>'
                                        + '<td class="price-all"></td>'
                                        + '<td><div class="am-form"><input type="text" name="" class="am-u-md-12 form-control remarks"></div></td>'
                                        + '<td><button type="button" class="am-btn am-btn-danger am-btn-xs delect-btn">删除</button></td>'
                                        +'</tr>';
                    
                }

                thisTr.after(specificationTr);

                for(var k = 0 ; k < $("#goodsMessage").find(".quantity").length; k++){
                    var quantityInp = $("#goodsMessage").find(".quantity").eq(k).val();
                    quantityInp = quantityInp == "" ? 0 : quantityInp;
                    oneGoodsNum += parseInt(quantityInp);
                }
                $("#quantity-all").text(oneGoodsNum);

                calcPriceAndNum();
            });
        });
    }

</script>

</body>

</html>